using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class Pages_Admin_ViewAccounts : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Session["AccountSelectMethod"] = "GetAllAccounts";
        }
        if (Session["AccountSelectMethod"] != null)
        {
            AccountObjectDataSource.SelectMethod = (string)Session["AccountSelectMethod"];
        }
    }    

    protected void btAddAccount_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Pages_Admin/ManageAccounts/AddAccount.aspx");
    }
    
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";
        txtSearch.Text = txtSearch.Text.Trim();
        if (txtSearch.Text == "") return;
        AccountObjectDataSource.SelectParameters.Clear();
        if (rdFullName.Checked) //Search by FullName:
        {
            Session["AccountSelectMethod"] = "SearchAccountByFullName";
            AccountObjectDataSource.SelectMethod = (string)Session["AccountSelectMethod"];
            AccountObjectDataSource.SelectParameters.Add("FullName", txtSearch.Text);
        }
        else if (rdBranchName.Checked) //Search by BranchName:
        {
            Session["AccountSelectMethod"] = "SearchAccountByBranchName";
            AccountObjectDataSource.SelectMethod = (string)Session["AccountSelectMethod"];
            AccountObjectDataSource.SelectParameters.Add("BranchName", txtSearch.Text);
        }
        gvAccounts.PageIndex = 0;
        gvAccounts.DataBind();
    }

    protected void gvAccounts_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvAccounts.PageIndex = e.NewPageIndex;
        gvAccounts.DataBind();
    }

    protected void btnDeleteAccount_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";
        int i;
        int lastCol = gvAccounts.Columns.Count - 1;
        CheckBox cbo;
        ArrayList userNames = new ArrayList();
        for (i = 0; i < gvAccounts.Rows.Count; i++)
        {
            cbo = gvAccounts.Rows[i].Cells[lastCol].Controls[1] as CheckBox;
            if (cbo.Checked)
            {
                userNames.Add(gvAccounts.Rows[i].Cells[0].Text);
            }
        }
        //Delete accounts:
        if (userNames.Count == 0)
        {
            lblMessage.Text = "Please select account to delete";
        }
        else
        {
            try
            {
                int result;
                result = DALAccount.DeleteAccounts((string[])userNames.ToArray(typeof(string)));
                gvAccounts.DataBind();
                lblMessage.Text = "Deleted successfully";
            }
            catch
            {
                lblMessage.Text = "Error deleting account(s)";
            }
        }
    }
}
